﻿using System.Collections.Generic;
using System.ComponentModel;

namespace Borg.Framework
{
    public class RegistredPlugsDataModel : IDataModel<PlugTypeInfo>
    {
        public event ListChangedEventHandler ListChanged;

        public RegistredPlugsDataModel(SlotInfo slot)
        {
            Slot = slot;

            Slot.Registered += Registered;
            Slot.Deregistered += Deregistered;
        }

        private void Deregistered(object sender, RegisterEventArgs args)
        {
            InvokeListChanged();
        }

        public IEnumerator<PlugTypeInfo> GetEnumerator()
        {
            return Slot.RegisteredPlugTypeInfos.GetEnumerator();
        }

        private void InvokeListChanged()
        {
            if (ListChanged == null)
                return;

            var e = new ListChangedEventArgs(ListChangedType.Reset, -1);
            ListChanged(this, e);
        }

        private void Registered(object sender, RegisterEventArgs args)
        {
            InvokeListChanged();
        }

        public int Count
        {
            get { return Slot.RegisteredPlugTypeInfos.Count; }
        }

        public PlugTypeInfo this[int index]
        {
            get { return Slot.RegisteredPlugTypeInfos[index]; }
        }

        private SlotInfo Slot { get; set; }
    }
}

